package com.kaifamiao.schedule;

public class YieldTest {

    public static void startThread(String name, int priority) {
        // 用匿名类继承Thread并重写run方法
        new Thread(name) {
            @Override
            public void run() {
                // 线程启动后首先更改优先级
                this.setPriority(priority);
                for (int i = 1; i < 6; i++) {
                    System.out.printf("%s 线程(priority: %d) - 第%d次\n", name, priority, i );
                    // try{ Thread.sleep(0L, 1); } catch ( InterruptedException e) {}
                    Thread.yield(); // 让位 给与自己同等优先级的线程或优先级比自己高的线程
                }
            }
        }.start();
    }

    public static void main(String[] args) {
        startThread("孙武", Thread.MIN_PRIORITY );
        startThread( "伍子胥", Thread.NORM_PRIORITY );
        startThread( "阖闾", Thread.MAX_PRIORITY );
        startThread( "勾践", Thread.MAX_PRIORITY );
    }

}
